<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <a-alert
        v-if="isLoginError"
        type="error"
        showIcon
        style="margin-bottom: 24px;"
        :message="errorMsg"
      />
      <div class="typeName">
        代理商管理平台
      </div>
      <div class="loginType">
        <div :class="loginType == 0? 'typeBox typeActive' : 'typeBox'"  @click="loginType = 0">账号登录</div>
        <div class="typeBox"
       @click="loginType = 1"
       :class="loginType == 1? 'typeBox typeActive' : 'typeBox'" 
        style="margin-right: 0;">手机号登录</div>
      </div>
      <div v-if="loginType == 0">
        <a-form-item style="margin-top: 30px;">
        <div class="accout">账号</div>
        <a-input
          size="large"
          type="text"
          style="border-radius: 6px!important;"
          placeholder="请输入帐号"
          :bordered="false"
          v-decorator="[
            'loginName',
            {rules: [{ required: true, message: '请输入帐号' }], validateTrigger: 'blur'}
          ]"
        >
          <!-- <template #prefix>
            <a-icon type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
          </template> -->
          
        </a-input>
      </a-form-item>

      <a-form-item>
        <div class="accout">密码</div>
        <a-input
        style="border-radius: 6px!important;"
          size="large"
          type="password"
          autocomplete="false"
          placeholder="请输入密码"
          v-decorator="[
            'password',
            {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
          ]"
        >
          <!-- <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/> -->
        </a-input>
      </a-form-item>
      </div>
      <div v-if="loginType == 1">
        <a-form-item style="margin-top: 30px;">
        <div class="accout">手机号</div>
        <a-input
          size="large"
          type="text"
          style="border-radius: 6px!important;"
          placeholder="请输入手机号"
          :bordered="false"
          v-decorator="[
            'phone',
            {rules: [{ required: true, message: '请输入手机号' }], validateTrigger: 'blur'}
          ]"
        >
          
        </a-input>
      </a-form-item>
      <a-form-item>
        <div class="accout">验证码</div>
        <div class="codeOut" style="position: relative;">
        
        <a-input
          size="large"
          type="text"
          autocomplete="false"
          placeholder="验证码"
          style="border-radius: 6px!important;"
          v-decorator="[
            'smsCode',
            {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}
          ]"
        >
        </a-input>
        <span style="position: absolute;right: 11px;color: #2961F2;font-size: 12px;cursor: pointer;" 
        @click="getCode()">{{btnText}}</span>
      </div>
      </a-form-item>
      </div>
      <a-form-item v-if="loginType == 0">
        <a-checkbox v-decorator="['rememberMe', { valuePropName: 'checked' }]">记住我</a-checkbox>
        <!-- <router-link
          :to="{ name: 'recover', params: { user: 'aaa'} }"
          class="forge-password"
          style="float: right;"
        >忘记密码</router-link>-->
      </a-form-item>

      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >确定</a-button>
      </a-form-item>
      <div class="footer">
      <div class="copyright">
          Copyright
          <a-icon type="copyright" /> 2024 青岛人才共享科技有限公司
        </div>
     </div>
      <!-- <div class="user-login-other">
        <router-link class="register" :to="{ name: 'register' }">注册用户名</router-link>
      </div>-->
    </a-form>
  </div>
</template>

<script>
// 此处的16跟上面的16对应，同样是倍数
const baseSize = 16
// 设置根节点的font-size大小函数
function setRem () {
    // 375为设计稿页面宽度，如为750，把375改为750
    const scale = document.documentElement.clientWidth / 1920
    // 设置页面根节点字体大小
    document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置根节点的font-size大小
window.onresize = function () {
    setRem()
}
import { mapActions } from 'vuex'
// import { timeFix } from '@/utils/util'
import {getPhoneCode} from '@/api/manage.js'
export default {
  data () {
    return {
      customActiveKey: 'tab1',
      loginBtn: false,
      isLoginError: false,
      form: this.$form.createForm(this),
      errorMsg: '',
      state: {
        loginBtn: false
      },
      loginType: 0,
      time: '',
      btnText: '获取验证码',
      timeo: 60,
      smsVerifyCode: '',
      phone: '',
      companyList: []
    }
  },
  mounted () {
    try {
      const savedData = localStorage.getItem('rememberMe')
      const loginParams = JSON.parse(atob(savedData))
      this.form.setFieldsValue(loginParams)
    } catch (error) {
      localStorage.removeItem('rememberMe')
    }
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        loginType,
        state,
        customActiveKey
      } = this

      state.loginBtn = true

      const validateFieldsKey =
        loginType === '0' ? ['loginName', 'password', 'rememberMe'] : ['phone', 'smsCode']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = this.form.getFieldsValue()
          this.doLogin(loginParams)
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    doLogin (loginParams) {
      // console.log(loginParams)
      // return
      this.Login(loginParams)
        .then(res => {
          this.loginSuccess(res)
          if (loginParams.rememberMe) {
            localStorage.setItem('rememberMe', btoa(JSON.stringify(loginParams)))
          } else {
            localStorage.removeItem('rememberMe')
          }
        })
        .catch(err => {})
        .finally(() => {
          this.state.loginBtn = false
        })
    },
    loginSuccess (res) {
      this.$router.push({ path: '/' })
      // 延迟 1 秒显示欢迎信息
      // setTimeout(() => {
      //   this.$notification.success({
      //     message: '欢迎',
      //     description: `${timeFix()}，欢迎回来`
      //   })
      // }, 1000)
      this.isLoginError = false
    },
    requestFailed (err) {
      this.isLoginError = true
      this.errorMsg = err.resData || err.resMsg || '请求出现错误，请稍后再试'
      localStorage.removeItem('rememberMe')
    },
    //  获取验证码 
    getCode () {
      // let timeo = 60
      const obj = this.form.getFieldsValue()
      console.log(obj.loginName)
      if (!obj.phone) {
        this.$message.error(`请输入手机号！`)
        return
      }
      if (this.timeo < 60) {
        this.$message.error(`验证码正在发送中...`)
        return
      }
			this.time = setInterval(() => {
				this.timeo--
				this.btnText = `${this.timeo}s后获取`
				if (this.timeo == 0) {
					clearInterval(this.time)
					this.btnText = '重新获取'
          this.timeo = 60
							// this.codeDisabled = false
					}
				}, 1000)
     
      getPhoneCode({phone: obj.phone, smsScene: 'SAAS_LOGIN'}).then(res => {
        if (res.resCode === '0000') {

        }
      }).error(err => {
        // this.$message.error(`请先完善发票信息`)
        clearInterval(this.time)
				this.btnText = '重新获取'
        this.timeo = 60
      })
    },
  }
}
</script>

<style lang="less" scoped>
.main{
  font-size: 12px;
}
input{
  border-radius: 10px!important;
}
.user-layout-login {
  background-color: #fff!important;
  border-radius: 20px;
  box-shadow: 0px 8px 24px 0px rgba(22,51,128,0.1);
  padding: 3.33rem;
  padding-bottom: 2.5rem;
  .typeName{
    color: #000000;
    font-size: 1.7rem;
    text-align: center;
  }
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 1.3333rem;
    height: 3.5rem;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}

.codeOut{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}
.ant-form-item{
  margin-bottom: 1rem!important;
  width: 26.1667rem;
  // height: ;
}
.codeInput{
  .ant-form-item{
    margin-bottom: 0!important;
  }
  .ant-form-item-control{
    width: 100%!important;
  }
  .ant-col{
    width: 100%!important;
  }
}
.spanOut{
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  margin-top: 1rem;
}
.blueText{
  color: #1890ff;
}
.loginType{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 1.6667rem;
  // margin-top: 90px;
  .typeBox{
    color: #A3A3A3;
    font-size: 1.2rem;
    margin-right: 8.33rem;
    padding-bottom: 8px;
    cursor: pointer;
  }
  .typeActive{
    color: #000000;
    position: relative;
  }
  .typeActive::after{
    position: absolute;
    content: "";
    width: 2.3333rem;
    height: 4px;
    background: #2961F2;
    border-radius: .5rem;
    bottom: 0;
    left: calc(50% - 1.1667rem);
  }
  p{
    margin-bottom: 0;
  }
  .accout{
    color: #9DA6B0!important;
    font-size: 1rem;
    margin-bottom: 0!important;
  }
}
.accout{
    color: #9DA6B0!important;
    font-size: 1rem;
    margin-bottom: 0!important;
    line-height: 1.8rem;
    // font-weight: bold;
  }
.main{
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 100vh;
}
.footer {
        position: absolute;
        width: 100%;
        bottom: 20px;
        left: 0;
        // padding: 0 16px;
        // margin: 48px 0 24px;
        text-align: center;

        .links {
          margin-bottom: 8px;
          font-size: 14px;
          a {
            color: rgba(0, 0, 0, 0.45);
            transition: all 0.3s;
            &:not(:last-child) {
              margin-right: 40px;
            }
          }
        }
        .copyright {
          color: rgba(0, 0, 0, 0.45);
          font-size: 14px;
        }
      }
      /deep/.ant-select{
        &::placeholder{
        color: rgba(0, 0, 0, 0.16)!important;
        font-size: 12px!important;
      }}
      /deep/.ant-select-selection{
        border-radius: .5rem!important;
        line-height: 3.5rem!important;
        height: 3.5rem!important;
        padding: 0!important;
      }
      /deep/.ant-select-selection__rendered{
        line-height: 3.5rem!important;
        height: 3.5rem!important;
        font-size: 1.1rem!important;
      }
      /deep/.ant-select-selection__placeholder, .ant-select-search__field__placeholder{
        color: rgba(0, 0, 0, 0.16)!important;
        font-size: 1rem!important;
        line-height: 3.5rem!important;
        height: 3.5rem!important;
        top: 0!important;
        margin-top: 0!important;
      }
     
      /deep/.ant-input{
        line-height: 3.5rem!important;
        height: 3.5rem!important;
        font-size: 1.1rem!important;
      }
      /deep/.ant-input{
  &::placeholder {
    color: rgba(0, 0, 0, 0.16)!important;
        font-size: 1rem!important;
        line-height: 3.5rem!important;
        height: 3.5rem;
  }
}
#formLogin{
  background-color: #fff;
}
.mask{
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.7);
}
/deep/.ant-modal-header{
  display: none!important;

}
/deep/.ant-modal-content{
  // padding: 3.33rem;
  padding: 0;
  width: 33rem;
  border-radius: 20px;
  overflow: hidden;
}
/deep/.ant-modal-body{
  padding: 0!important;
}
/deep/.ant-modal-footer{
  display: none!important;
}
.sendCodes{
  font-size: 1rem;
  color: #000;
  line-height: 1rem;
}
.findPsd{
  color: #333333;
  font-size: 1.6rem;
  position: absolute;
  top: 2.2rem;
}
/deep/.ant-modal-close{
  margin-top: .01rem;
}
</style>
